import request from '../../utils/request';
const user = {
  state: {
    token: null,
    user: '88988'
  },

  getters: {
    user: state => state.user,  
  },

  mutations: {
    SET_TOKEN(state, token) {
      state.token = token;  
    }
  },

  actions: {
    /** 
     * 资金账户修改
     * @params payload{
     *  id string  资金明细里的id
     *  usableAmount string  可用余额
     *  frozenAmount string 冻结资金
     *  totalAmountHistory string  历史余额（只增不减）
     *  withdrawAmountHistory string 	历史提现金额数量（只增不减）
     * }
    */
    UPDATE_USER_BILL({ commit }, payload) {
      return new Promise((resolve, reject) => {
        request.post('/capital/save.htm', payload).then(res => {
          console.log('commit::', commit);
          resolve(res);
        }).catch(err => {
          console.error('err::::', err);  
          reject(err);
        })
      });
    },
    /** 
     * 根据用户id获取用户资金账户明细
     * @param payload{
     *  userId string 
     * }
     * */ 
    GET_USER_BILL_BY_USER_ID({ commit }, payload) {
      return new Promise((resolve, reject) => {
        request.post('/capital/detail.htm', payload).then(res => {
          console.log('commit::', commit);
          resolve(res);
        }).catch(err => {
          console.error('err::::', err);  
          reject(err);
        })
      });
    },
    // 用户退出登录
    LOGOUT({ commit }) {
      return new Promise((resolve, reject) => {
        request.post('/employee/logout.htm').then(res => {
          commit('SET_ROUTERlIST', null);
          resolve(res);
        }).catch(err => {
          console.error('err::::', err);  
          reject(err);
        })
      });
    },
    // 用户名登录
    LOGIN({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
        request.post('/employee/login.htm', userInfo).then(res => {
          console.log('res::', res);
          console.log('commit::', commit);
          // commit('SET_TOKEN', res.data.token);
          resolve(res);
        }).catch(err => {
          console.error('err::::', err);  
          reject(err);
        })
      });
    },

    // 获取用户列表
    GET_USER_LIST({ commit }, payload) {
      return new Promise((resolve, reject) => {
        request.post('/user/list.htm', payload).then(res => {
          console.log('res:GET_USER_LIST----------------:', res);
          console.log('commit::', commit);
          // commit('SET_TOKEN', res.data.token);
          resolve(res);
        }).catch(err => {
          console.error('err::::', err);  
          reject(err);
        })
      });
    },

    /**
     * 更新用户信息
     * @params payload{
     *  id string required
     *  mobileNo string
     *  password string 
     *  idCardNo string
     *  state    string   状态（可选）
     *  licenseNo string  执照号码（可选）
     *  nickName  string
     *  isVip     string
     * }
     * */
    UPDATE_USER({ commit }, payload) {
      return new Promise((resolve, reject) => {
        request.post('/user/update.htm', payload).then(res => {
          console.log('commit::', commit);
          resolve(res);
        }).catch(err => {
          console.error('err::::', err);  
          reject(err);
        })
      });
    },

    /**
     * 通用-用户查询
     * @param id string
    */
    QUERY_USER({ commit }, payload) {
      return new Promise((resolve, reject) => {
        request.post('/user/query.htm', payload).then(res => {
          console.log('commit::', commit);
          resolve(res);
        }).catch(err => {
          console.error('err::::', err);  
          reject(err);
        })
      });
    }
  }
}

export default user